<?php
$xmlstring = '
	<ShareInfo UserName="rafale@thetigerparty.com" MediaID="" Message="Test" WebLink="http://www.amazon.com/"><facebook ID="100001690511262" token="BAAFdqWJjZCLUBAGDFFKIRgWeQvu7fuZANvdBGuZBjSmBio4LU667ctWlBhwIu2MDZAOPZCZBqQhqZBfCkk0gMNKrs1WQ16nUu9Bao59MyhOK8N4GwPPd7CcDINCHSVHB3sZD" /><twitter key="" secret="" /><instagram token="" /></ShareInfo>';

require '../Facebook/FBsdk/src/facebook.php';


$xml = simplexml_load_file('php://input');
//$xml = simplexml_load_string($xmlstring);

foreach( $xml->attributes( ) as $attr)
{
	$AttrArray[] = $attr;
}
$Title = $AttrArray[2];
$Description = $AttrArray[3];
$WebLink = $AttrArray[4];
$ImageURL = $AttrArray[5];
$MediaType = $AttrArray[6];

foreach( $xml->children() as $child)
{
//					var_dump($child);
		switch ($child->getName())
		{
			case "facebook":
				foreach( $child->attributes( ) as $attr)
					$Facebook[] = (string)$attr;
				break;
			case "twitter":
				foreach( $child->attributes( ) as $attr)
					$Twitter[] = (string)$attr;
				break;
			case "tumblr":
				foreach( $child->attributes( ) as $attr)
					$Tumblr[] = (string)$attr;
				break;
			case "instagram":
				foreach( $child->attributes( ) as $attr)
					$Instagram[] = (string)$attr;
				break;
		}
}
if(isset($Facebook) && strlen($Facebook[0]) > 0)
{
	
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '250527955081918',
  'secret' => '8b4a436bc902681ded48cbf5eeffe760',
  'fileUpload' => true,
));
// Get User ID
$facebook->setAccessToken($Facebook[1]);
$user = $facebook->getUser();

if ($user) {
  try {
	 
	  $attachment = array( 
	  'message' => (string)$Title,
	  'description' => (string)$Description,
	  'link' => (string)$WebLink,
	  'source' => (string)$ImageURL
	  );
	  $result = $facebook->api('/me/feed/','post',$attachment);
	  
	  if ($result)
	  	echo "<Success Message=\"The requested Information was accepted\" Domain=\"BeShare\"/>";
	  else
	  	echo "<Error Message=\"The requested Information was not found\" Domain=\"BeShare\"/>";
  } 
  catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}


}

	function XMLCharEncode( $Subject )
	{
		$Serach  = array( "<", ">", "&", "'","\"" );
		$Replace = array( "&lt;","&gt;","&amp;","&apos;","&quot;");
		return str_replace($Serach, $Replace, $Subject);
	}

?>
